package com.bookbuf.app;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.bookbuf.library.LoaderLayout;
import com.bookbuf.library.LoaderResult;
import com.bookbuf.library.adapter.impl.LoaderResultAdapter;

public class WebViewActivity extends AppCompatActivity {

	public static final String TAG = "WebViewActivity";

	LoaderLayout loaderLayout;
	LoaderResultAdapter adapter;
	LoaderResult<Boolean> result = new LoaderResult<> ();
	WebView webView;

	private WebViewClient client = new WebViewClient () {
		@TargetApi (Build.VERSION_CODES.M)
		@Override
		public void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error) {
			super.onReceivedError (view, request, error);
			Log.i (TAG, "onReceivedError: " + error.getErrorCode () + ":" + error.getDescription ());
			result.setSuccess (false);
		}

		@Override
		public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
			super.onReceivedError (view, errorCode, description, failingUrl);
			Log.i (TAG, "onReceivedError: " + errorCode + ":" + description);
			result.setSuccess (false);
		}

		@TargetApi (Build.VERSION_CODES.LOLLIPOP)
		@Override
		public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
			super.onReceivedHttpError (view, request, errorResponse);
			Log.i (TAG, "onReceivedError: " + errorResponse.getStatusCode () + ":" + errorResponse.getReasonPhrase ());
			result.setSuccess (false);
		}

		@Override
		public boolean shouldOverrideUrlLoading (WebView view, String url) {
			view.loadUrl (url);
			Log.i (TAG, "shouldOverrideUrlLoading: url = " + url);
			return true;
		}

		@Override
		public void onPageStarted (WebView view, String url, Bitmap favicon) {
			super.onPageStarted (view, url, favicon);
			setLoading ();
			Log.i (TAG, "onPageStarted: setLoading");
		}

		@Override
		public void onPageFinished (WebView view, String url) {
			super.onPageFinished (view, url);

			if (result.isSuccess () == null || result.isSuccess ()) {
				Log.i (TAG, "onPageFinished: setSuccess");
				setSuccess ();
			} else {
				setFail ();
				Log.i (TAG, "onPageFinished: setFail");
			}

		}

		// =============================
		// 模板方法
		// =============================

		private void setLoading () {
			result.setSuccess (null);
			result.setData (null);
			adapter.setResult (result);
		}

		private void setSuccess () {
			result.setSuccess (true);
			result.setData (true);
			adapter.setResult (result);
		}

		private void setFail () {
			result.setSuccess (false);
			result.setData (null);
			adapter.setResult (result);
		}
	};

	public static void callMe (Context context) {
		context.startActivity (new Intent (context, WebViewActivity.class));
	}

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_webview);

		adapter = new LoaderResultAdapter ();
		loaderLayout = (LoaderLayout) findViewById (R.id.loading_layout);
		if (loaderLayout != null) {
			loaderLayout.setPreInitContent (true);
			loaderLayout.setInteractionListener (new LoaderLayout.InteractionListener () {

				@Override
				public void onInflateContentView (View view, LoaderResult<?> loaderResult) {

					Log.e (LoaderLayout.TAG, "*** onInflateContentView: onInflateContentView");
					webView = (WebView) view.findViewById (R.id.content_webView);
					webView.loadUrl ("https://git.oschina.net/");
					webView.setWebViewClient (client);
				}

				@Override
				public void onEventRetry () {
					adapter.setLoading ();
				}
			});
			loaderLayout.setAdapter (adapter);
			adapter.setLoading ();
		}
	}

	@Override
	public void onBackPressed () {
		if (webView.canGoBack ()) {
			webView.goBack ();
			Log.e (TAG, "onBackPressed: goBack()");
		} else {
			super.onBackPressed ();
			Log.e (TAG, "onBackPressed: finish()");
		}
	}
}
